package com.bw.educate.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class TemplateFreemark {



    public static void gethtmlTemplate(Map<String,Object> map, String name, String path) {
        //使用Freeemarker 的配置工具类
        //不需要执行 详细的文件
        Configuration configuration = new Configuration(Configuration.getVersion());  //模板引擎配置类

        File file = new File(path);

        try {
            configuration.setDirectoryForTemplateLoading(file);
            //设置编码格式
            configuration.setDefaultEncoding("utf-8");
            //获取具体的模板
            Template template = configuration.getTemplate(name+".html");
            String htmlStr = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);

            InputStream inputStream = IOUtils.toInputStream(htmlStr,"utf-8");
            //读取文件的位置 输出流
            FileOutputStream fileOutputStream = new FileOutputStream(path+"\\"+name+".html");

            IOUtils.copy(inputStream, fileOutputStream);

            fileOutputStream.close();
            inputStream.close();
        } catch (IOException | TemplateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}
